package com.itboye.boyeshoping.base

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import com.itboye.boyeshoping.util.Malert
import com.itboye.boyeshoping.util.volley.ResultEntity

/**
 * Created by Administrator on 2018/6/27 0027.
 */
abstract class BaseFragment : Fragment() {

//    internal var progressDialog: ProgressDialog? = null
    //	private PushAgent mPushAgent;
    val EVENT_TYPE_UNKNOWN = BaseActivity::class.java!!
            .getName() + "_unknown"

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        //		mPushAgent = PushAgent.getInstance(getActivity());
        //		mPushAgent.enable();
        //		mPushAgent.onAppStart();
        smartInject(view)
//        progressDialog = ProgressDialog()
        onMyActivityCreated()
    }


    protected fun smartInject(view: View?) {
        try {
            var clz: Class<out Fragment> = javaClass
            while (clz != BaseFragment::class.java) {
                val fs = clz.declaredFields
                val res = resources
                val packageName = activity!!.packageName
                for (field in fs) {
                    if (!View::class.java.isAssignableFrom(field.type)) {
                        continue
                    }
                    val viewId = res.getIdentifier(field.name, "id",
                            packageName)
                    if (viewId == 0)
                        continue
                    field.isAccessible = true
                    try {
                        val v = view!!.findViewById<View>(viewId)
                        field.set(this, v)
                        val c = field.type
                        val m = c.getMethod("setOnClickListener",
                                android.view.View.OnClickListener::class.java)
                        m.invoke(v, this)
                    } catch (e: Throwable) {
                    }

                    field.isAccessible = false

                }

                clz = clz.superclass as Class<out Fragment>
            }
        } catch (e: Exception) {
            // TODO Auto-generated catch block
            e.printStackTrace()
        }

    }


    /**
     * @param data
     * @return 2016-4-23 hebidu
     */
    fun handlerError(data: Any): ResultEntity {
        var result: ResultEntity? = data as ResultEntity
        if (result == null) {
            result = ResultEntity("-1", "数据格式错误!~", data)
            return result
        }

        if (result!!.hasError()) {

            if (result!!.getMsg().contains("java.net.UnknownHostException")) {
                Malert.alert("请连接网络 ")
            } else {
                Malert.alert(result!!.getMsg())
            }
            result!!.setEventType(EVENT_TYPE_UNKNOWN)
            return result
        }

        return result
    }

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // TODO Auto-generated method stub
        return inflater.inflate(initView(), null)
    }

    abstract fun initView(): Int

    abstract fun onMyActivityCreated()

    /**
     * 显示进度对话框,带有消息 flag 是否可以取消
     */
//    @SuppressLint("NewApi")
//    fun showProgressDialog(message: String, flag: Boolean) {
//        if (progressDialog == null)
//            return
//        setProgressDialogMessage(message)
//        if (progressDialog!!.isAdded()) {
//
//        } else {
//            progressDialog!!.show(activity!!.fragmentManager, message)
//            progressDialog!!.setCancelable(flag)
//        }
//
//    }
//
//    /**
//     * 设置进度对话框消息
//     *
//     * @param message
//     */
//    fun setProgressDialogMessage(message: String) {
//        progressDialog!!.setMsg(message)
//    }
//
//    /**
//     * 关闭进度对话框
//     */
//    @SuppressLint("NewApi")
//    fun closeProgressDialog() {
//        if (progressDialog != null) {
//            if (progressDialog!!.getDialog().isShowing()) {
//                progressDialog!!.dismiss()
//            }
//        }
//    }

    fun getAppVersionName(context: Context): String? {
        var versionName: String? = ""
        try {
            // ---get the package info---
            val pm = context.packageManager
            val pi = pm.getPackageInfo(context.packageName, 0)
            versionName = pi.versionName

            if (versionName == null || versionName.length <= 0) {
                return ""
            }
        } catch (e: Exception) {
            Log.e("VersionInfo", "Exception", e)
        }

        return versionName
    }

    fun onTouchEvent(event: MotionEvent): Boolean {
        // TODO Auto-generated method stub
        return false
    }

    /**
     * 设置状态栏高度
     * @param
     */
    fun setStatusBarHeight(view: View) {
        val params = view.layoutParams
        params.height = getStatusBarHeight()
        view.layoutParams = params
    }

    /**
     * 获取状态栏高度
     * @return
     */
    fun getStatusBarHeight(): Int {
        //获取status_bar_height资源的ID
        val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            resources.getDimensionPixelSize(resourceId)
        } else 0
    }
}